package com.onlyphoto.onlymall.widget

import android.annotation.SuppressLint
import android.content.Context
import android.os.CountDownTimer
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.utils.Constants.CODE_OVER_TIME


/**
 * @author resonLei
 * @date 2018/7/11
 * @desc:用于获取验证码的倒计时view
 */

class VerifyCodeButton : AppCompatTextView {
    private val defaultMillisInFuture: Long = CODE_OVER_TIME * 1000L
    private var mTimer: MyCountDownTimer? = null
    private var finishStr: String? = null
    private var onTouchDownTimeListener: OnTouchDownTimeListener? = null

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initView(attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView(attrs)
    }

    private fun initView(attrs: AttributeSet) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.VerifyCodeButton)
        finishStr = typedArray.getString(R.styleable.VerifyCodeButton_finishText)
        if (finishStr?.isEmpty() != false) {
            finishStr = "重新发送"
        }
        typedArray.recycle()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        mTimer?.cancelTimer()
    }

    fun startCountDownTime() {
        startCountDownTime(defaultMillisInFuture)
    }

    fun startCountDownTime(ms: Long) {
        isEnabled = false
        mTimer = MyCountDownTimer(ms, 1000)
        mTimer?.start()
    }

    fun setText(str: String, enable: Boolean) {
        isEnabled = enable
        text = str
    }

    fun reset() {
        mTimer?.cancelTimer()
        text = finishStr
        isEnabled = true
    }

    override fun setEnabled(enabled: Boolean) {
        if (mTimer?.isRuning() != true) {
            super.setEnabled(enabled)
        }
    }

    inner class MyCountDownTimer(millisInFuture: Long, countDownInterval: Long) : CountDownTimer(millisInFuture, countDownInterval) {
        private var isRunning = false

        @SuppressLint("SetTextI18n")
        override fun onTick(millisUntilFinished: Long) {
            val second = (millisUntilFinished / 1000).toInt()
            text = second.toString() + "秒"

            onTouchDownTimeListener?.onTick(millisUntilFinished)
            isRunning = true
        }

        override fun onFinish() {
            isRunning = false
            text = finishStr
            isEnabled = true

            onTouchDownTimeListener?.onFinish()
        }

        /**
         * 优先使用该方法来结束timer，如果你需要在cancel之后进行其他操作的话
         */
        fun cancelTimer() {
            isRunning = false
            this.cancel()
        }

        fun isRuning(): Boolean {
            return isRunning
        }

    }

    interface OnTouchDownTimeListener {
        fun onFinish()
        fun onTick(millisUntilFinished: Long)
    }

    fun setOnTouchDownTimeListener(onTouchDownTimeListener: OnTouchDownTimeListener) {
        this.onTouchDownTimeListener = onTouchDownTimeListener
    }
}
